//
//  UIImage+WBImage.m
//  WBChat
//
//  Created by RedRain on 2018/1/19.
//  Copyright © 2018年 RedRain. All rights reserved.
//

#import "UIImage+WBImage.h"

@implementation UIImage (WBImage)

+ (UIImage *)wb_userHeaderPlaceholderImage{
    return [self wb_resourceImageNamed:@"header_male"];
}
+ (UIImage *)wb_resourceImageNamed:(NSString *)name{
    //先从默认目录里读
    UIImage *imageFromMainBundle = [UIImage imageNamed:name];
    if (imageFromMainBundle) {
        return imageFromMainBundle;
    }
    //读不到再去Bundle里读
    //此处Scale是判断图片是@2x还是@3x
    NSInteger scale = (NSInteger)[[UIScreen mainScreen] scale];
    for (NSInteger i = scale; i >= 1; i--) {
        NSString *filepath = [self getImagePath:name scale:i];
        UIImage *tempImage = [UIImage imageWithContentsOfFile:filepath];
        if (tempImage) {
            return tempImage;
        }
    }
    return nil;
}

+ (NSString *)getImagePath:(NSString *)name scale:(NSInteger)scale{
    static NSBundle *resBundle = nil;
    if (resBundle == nil) {
        // 这里不使用mainBundle是为了适配pod 1.x和0.x
        resBundle = [self bundleUrlWithResourcsName:@"WBUI"];
    }
    NSBundle *customBundle = resBundle;
    NSString *bundlePath = [customBundle bundlePath];
    NSString *imgPath = [bundlePath stringByAppendingPathComponent:name];
    NSString *pathExtension = [imgPath pathExtension];
    //没有后缀加上PNG后缀
    if (!pathExtension || pathExtension.length == 0) {
        pathExtension = @"png";
    }
    //Scale是根据屏幕不同选择使用@2x还是@3x的图片
    NSString *imageName = nil;
    if (scale == 1) {
        imageName = [NSString stringWithFormat:@"%@.%@", [[imgPath lastPathComponent] stringByDeletingPathExtension], pathExtension];
    }
    else {
        imageName = [NSString stringWithFormat:@"%@@%ldx.%@", [[imgPath lastPathComponent] stringByDeletingPathExtension], (long)scale, pathExtension];
    }
    //返回删掉旧名称加上新名称的路径
    return [[imgPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:imageName];
}

+ (NSBundle *)bundleUrlWithResourcsName:(NSString *)name{
    NSString * bundlePath = [[ NSBundle mainBundle]pathForResource:name ofType:@"bundle"];
    NSBundle *resourceBundle = [NSBundle bundleWithPath:bundlePath];
    return resourceBundle;
}

//获取图片大小
+ (CGSize)imageSizeWithImage:(UIImage *)image{
    
    float kDialogHeaderImageMaxHeight = 150;
    float kDialogHeaderImageMinHeight= 80;
    float kDialogHeaderImageMaxWidth = 150;
    float kDialogHeaderImageMinWidth = 80;
    
    CGSize imageSize = CGSizeMake(100, 100);
    
    if (image) {
        imageSize = CGSizeMake(image.size.width, image.size.height);
        
    }
    
    
    if (imageSize.height > imageSize.width) {
        CGFloat imageWidth = (kDialogHeaderImageMaxHeight / imageSize.height) * imageSize.width;
        if (imageWidth < kDialogHeaderImageMinWidth) {
            imageWidth = kDialogHeaderImageMinWidth;
        }
        imageSize = CGSizeMake(imageWidth,kDialogHeaderImageMaxHeight);
        
    }else{
        CGFloat imageHeight = (imageSize.height / imageSize.width) * kDialogHeaderImageMaxWidth;
        if (imageHeight < kDialogHeaderImageMinHeight) {
            imageHeight = kDialogHeaderImageMinHeight;
        }
        imageSize = CGSizeMake(kDialogHeaderImageMaxWidth, imageHeight);
    }
    
    return imageSize;
}
@end
